package cn.itcast.service;

import cn.itcast.dao.MemberMapper;
import cn.itcast.dao.OrderMapper;
import cn.itcast.dao.ReportMapper;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ReportMapper reportMapper;

    /**
     * 运营数据统计
     *
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberMapper.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderMapper.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderMapper.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderMapper.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);
        return result;
    }

    @Override
    public List<Map<String, Object>> findMemberReportBySex() {
        //result{flag:x,message:x,data:[{name:'男性',value:5},{name:'女性',value:7}]}
        //调用mapper
        Map<String, Integer> map = reportMapper.findMemberCountBySex();
        //设置返回值
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> hashMap01 = new HashMap<>();
        hashMap01.put("name", "男性");
        hashMap01.put("value", map.get("男性"));
        list.add(hashMap01);
        Map<String, Object> hashMap02 = new HashMap<>();
        hashMap02.put("name", "女性");
        hashMap02.put("value", map.get("女性"));
        list.add(hashMap02);
        return list;
    }

    @Override
    public Map<String, List> findMemberReportByAge() {
        //result{flag:x,message:x,data:[{name:'0-18',value:5},{name:'18-30',value:7}...]}
        Map<String, List> hashMap = new HashMap<>();
        //memberCount
        List<Map<String, Object>> list = new ArrayList<>();
        //ages
        List<String> ages = new ArrayList<>();

        //封装查询条件
        Map<String, Date> orders = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        //today/0
        //0
        Date time00 = calendar.getTime();
        orders.put("time00", time00);
        //0-18/18
        //18
        calendar.add(Calendar.YEAR, -18);
        Date time18 = calendar.getTime();
        orders.put("time18", time18);
        //18-30/12
        //30
        calendar.add(Calendar.YEAR, -12);
        Date time30 = calendar.getTime();
        orders.put("time30", time30);
        //30-45/15
        //45
        calendar.add(Calendar.YEAR, -15);
        Date time45 = calendar.getTime();
        orders.put("time45", time45);
        //45-60/15
        //60
        calendar.add(Calendar.YEAR, -15);
        Date time60 = calendar.getTime();
        orders.put("time60", time60);
        //60-80/20
        //80
        calendar.add(Calendar.YEAR, -20);
        Date time80 = calendar.getTime();
        orders.put("time80", time80);
        //80-100/20
        //100
        calendar.add(Calendar.YEAR, -20);
        Date time100 = calendar.getTime();
        orders.put("time100", time100);

        //调用mapper
        Map<String, Integer> result = reportMapper.findMemberCountByAge(orders);

        //设置返回值
        //0-18
        Map<String, Object> map18 = new HashMap<>();
        map18.put("name", "年龄段：0-18");
        map18.put("value", result.get("0to18"));
        list.add(map18);
        //18-30
        Map<String, Object> map30 = new HashMap<>();
        map30.put("name", "年龄段：18-30");
        map30.put("value", result.get("18to30"));
        list.add(map30);
        //30-45
        Map<String, Object> map45 = new HashMap<>();
        map45.put("name", "年龄段：30-45");
        map45.put("value", result.get("30to45"));
        list.add(map45);
        //45-60
        Map<String, Object> map60 = new HashMap<>();
        map60.put("name", "年龄段：45-60");
        map60.put("value", result.get("45to60"));
        list.add(map60);
        //60-80
        Map<String, Object> map80 = new HashMap<>();
        map80.put("name", "年龄段：60-80");
        map80.put("value", result.get("60to80"));
        list.add(map80);
        //80-100
        Map<String, Object> map100 = new HashMap<>();
        map100.put("name", "年龄段：80-100");
        map100.put("value", result.get("80to100"));
        list.add(map100);

        //设置memberCount
        hashMap.put("memberCount", list);

        //遍历list集合
        for (Map<String, Object> map : list) {
            ages.add((String) map.get("name"));
        }
        //设置ages
        hashMap.put("ages", ages);

        return hashMap;
    }
}
